layui.define(['jquery', 'layer'], function (exports) {
    "use strict";

    var $ = layui.jquery;
    var layer = layui.layer;

    var http = {};
    http.contentType = {
        form: "application/x-www-form-urlencoded;charset=UTF-8",
        json: "application/json; charset=utf-8",
        xml: "application/xml; charset=utf-8"
    }

    http.request = function (options) {
        // 使用Deferred对象创建一个新的Promise
        var deferred = $.Deferred();
        let loading = layer.load();
        let data = undefined
        let processData = true

        if (options.data instanceof FormData) {
            // 如果是FormData，禁用 processData 和 设置正确的contentType
            processData = false;
        } else if (typeof options.data === 'object' && options.contentType === http.contentType.json) {
            // 如果是JSON数据，需要转换为字符串并设置正确的contentType
            data = JSON.stringify(options.data);
        }
        else{
            data = options.data
        }
        $.ajax({
            url: options.url,
            headers:options.headers,
            dataType: options.dataType || 'json',   // 服务的响应的内容格式 默认为 json
            traditional: true,   // 序列化方式 true:key=value1&key=value2的格式  默认 key[]=value1&key[]=value2
            contentType: options.contentType || http.contentType.form, // 设置Content-Type为json
            data: data,
            processData: processData,
            type: options.method || 'post',
            success: function (result) {
                layer.close(loading);
                if (!result.success) {
                    layer.msg(result.message, {
                        icon: 2,
                        time: 3000
                    });
                    deferred.reject(result)
                }else{
                    deferred.resolve(result)
                }
            },
            error: function (err) {
                console.error(err)
                layer.close(loading);
                deferred.reject(err)
            }
        })
        return deferred.promise();
    }

    http.get = function (url,params){
        let loading = layer.load();
        return new Promise((resolve,reject) => {
            $.get(url,params,function (res){
                layer.close(loading);
                if(res.success){
                    resolve(res)
                }
                else{
                    reject(res)
                }
            })
        })
    }

    http.post = function (url,data){
        let loading = layer.load();
        return new Promise((resolve,reject) => {
            $.post(url,data,function (res,status,xhr){
                console.log(res,status,xhr)
                layer.close(loading);
                if(res.success){
                    resolve(res)
                }
                else{
                    reject(res)
                }
            })
        })
    }

    /**
     * 使用ajax下载文件
     * @param url 下载地址
     * @param data 传递的数据
     * @returns {Promise<unknown>} 异步对象
     */
    http.downFile = function (url,data) {
        let loading = layer.load();
        return new Promise((resolve) => {
            $.ajax({
                url: url,
                type: "post",
                data: data,
                xhrFields: {responseType: "blob"},
                success: function (res) {
                    layer.close(loading);
                    resolve(res)
                },
                error: function (err) {
                 console.error(err)
                }
            })
        })
    }

    exports('http', http);
});